<template>
  <div>
    <el-card>
      <el-form ref="form"
               :model="idFrom"
               :inline="true"
               label-width="100px"
               size="mini">
        <el-form-item label="账号">
          <el-input v-model.trim="idFrom.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model.trim="idFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model.trim="idFrom.mobel"></el-input>
        </el-form-item>

        <el-form-item size="mini">
          <el-button type="primary"
                     @click="queryUserInfonation">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-button type="primary"
               class="newId"
               @click="dialogVisible=true">新增账号</el-button>

    <el-card>
      <el-table :data="idList"
                border
                stripe
                style="width: 100%"
                @row-click="btnddd">
        <el-table-column prop="ids_id"
                         label="账号">
        </el-table-column>
        <el-table-column prop="id_name"
                         label="用户名">
        </el-table-column>
        <el-table-column prop="id_iphone"
                         label="联系电话">
        </el-table-column>
        <el-table-column prop="post"
                         label="职位">
        </el-table-column>
        <el-table-column label="状态">
          <!-- <template slot-scope="scope">
            <el-switch v-model="scope.row.operation_state"
                       @click="changeState(scope.row)">

            </el-switch>
          </template> -->
          <template v-slot="scope">
            <div>
              {{scope.row.operation_state?'启用':'停用'}}
            </div>
          </template>

        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="scope">
            <el-link type="success"
                     @click="changeDialogVisible=true">编辑</el-link>
            <el-link type="warning"
                     @click="resetPassword">重置密码</el-link>
            <el-link type="primary"
                     @click="changeState(scope.row)">{{scope.row.operation_state?'停用':'启用'}}</el-link>
            <el-link type="danger"
                     @click="delBtn">删除</el-link>
          </template>

        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加账号模块 -->
    <el-dialog title="新增/编辑帐号"
               :visible.sync="dialogVisible"
               @close="addDialogClose"
               width="30%">
      <el-form :model="userForm"
               :rules="rules"
               ref="userForm"
               label-width="100px">
        <el-form-item label="账号"
                      type="number">
          <el-input v-model.trim="userForm.ids_id"></el-input>
        </el-form-item>
        <el-form-item label="用户名"
                      type="index">
          <el-input v-model.trim="userForm.id_name"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="password">
          <el-input v-model.trim="userForm.password"
                    type="password"></el-input>
        </el-form-item>
        <el-form-item label="手机号"
                      prop="mobile">
          <el-input v-model.trim="userForm.id_iphone"></el-input>
        </el-form-item>
        <el-form-item label="职位"
                      prop="post">
          <el-input v-model.trim="userForm.post"></el-input>
        </el-form-item>
        <span class="qx">权限</span>
        <el-tree :data="data"
                 show-checkbox
                 default-expand-all
                 node-key="id"
                 ref="tree"
                 highlight-current
                 :props="defaultProps">
        </el-tree>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑账号模块 -->
    <el-dialog title="新增/编辑帐号"
               :visible.sync="changeDialogVisible"
               @close="changeDialogClose"
               width="30%">
      <el-form :model="changeUserForm"
               :rules="changeRules"
               ref="changeUserForm"
               label-width="100px">
        <el-form-item label="用户名"
                      prop="name">
          <el-input v-model.trim="changeUserForm.id_name"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="password">
          <el-input v-model.trim="changeUserForm.password"
                    type="password"></el-input>
        </el-form-item>
        <el-form-item label="手机号"
                      prop="mobile">
          <el-input v-model.trim="changeUserForm.id_iphone"></el-input>
        </el-form-item>
        <el-form-item label="职位"
                      prop="apost">
          <el-input v-model.trim="changeUserForm.post"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="changeDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addAccountNumber">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {

  },
  components: {

  },
  data () {
    var checkMobile = (rule, value, callback) => {
      const regMobile = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
      if (regMobile.test(value)) {
        return callback()
      }
      callback(new Error('请输入正确手机号'))
    }
    return {
      id: 0,
      idFrom: {
        id: '',
        name: '',
        mobel: ''
      },
      idList: [],
      idListTwo: [],
      leng: 0,
      userForm: {
        ids_id: '',
        id_name: 'aaaa',
        id_iphone: '18177777777',
        post: '董事长',
        password: '111111'
      },
      dialogVisible: false,
      changeDialogVisible: false,
      resetDialogVisible: false,
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        post: [
          { required: true, message: '请输入职位', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ]
      },
      data: [{
        id: 1,
        label: '店铺管理',
        children: [{
          id: 4,
          label: ' 店铺列表'
        }, {
          id: 7,
          label: ' 商品列表'
        }, {
          id: 8,
          label: ' 开店审核'
        }, {
          id: 9,
          label: ' 店铺权益管理'
        }]
      },
      {
        id: 2,
        label: '商圈管理',
        children: [{
          id: 5,
          label: '共享商圈'
        }, {
          id: 6,
          label: '个人商圈'
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      changeUserForm: {},
      changeRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        apost: [
          { required: true, message: '请输入职位', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getUser()
  },
  methods: {
    async getUser () {
      const { data } = await this.$http.get('/ids')
      console.log(data)
      this.idList = data.list
      this.idListTwo = data.list
      this.leng = this.idList.length
    },
    addDialogClose () {
      this.$refs.userForm.resetFields()
    },
    changeDialogClose () {
      this.$refs.changeUserForm.resetFields()
    },

    // 停用
    changeState (row) {
      row.operation_state = !row.operation_state
    },

    // 删除
    async delBtn () {
      const arr = []
      // 询问是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('以取消删除')
      }
      this.idList.forEach((item, index) => {
        if (this.id !== item.id_user) {
          arr.push(item)
        }
      })
      this.idList = arr
      this.$message.success('删除成功')
    },
    // 重置按钮
    reset () {
      this.idFrom.id = ''
      this.idFrom.name = ''
      this.idFrom.mobel = ''
      this.getUser()
    },
    // 查询
    queryUserInfonation () {
      this.idList = this.idListTwo
      if (this.idFrom.id || this.idFrom.name || this.idFrom.mobel) {
        const arr = []
        this.List = this.list
        this.idList.forEach((item, index) => {
          if (item.ids_id === this.idFrom.id * 1 || item.id_name === this.idFrom.name || item.id_iphone === this.idFrom.mobel) {
            arr.push(item)
          }
        })
        this.idList = arr
      }
    },
    btnddd (val) {
      this.id = val.id_user
      this.changeUserForm = val
    },
    // 编辑账号
    async addAccountNumber () {
      this.$message.success('编辑成功')
      this.changeDialogVisible = false
    },
    // 添加账号
    addUser () {
      this.idList.push(this.userForm)
      this.dialogVisible = false
    },
    // 重置密码
    async resetPassword () {
      const confirmResult = await this.$confirm('此操作将重置该用户的密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('以取消重置')
      }
      this.$message.success('以重置为初始密码')
    }
  }
}
</script>

<style lang="less" scoped>
.newId {
  margin: 20px 0 0 15px;
}
/deep/.el-table th > .cell {
  text-align: center;
}

/deep/.el-table .cell {
  text-align: center;
}
.el-tree {
  margin-left: 150px;
}
.qx {
  margin: 0px 0 0 50px;
  font-size: 15px;
}
</style>
